﻿// <copyright file="SplintLauncher.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Splint launcher.
// </summary>

namespace DoxyOutParser.Launcher
{
	using System.Collections.Generic;
	using System.Diagnostics;
	using DoxyOutParser.WorkingSet;

	/// <summary>
	/// Launches Splint with the appropriate options.
	/// </summary>
	internal class SplintLauncher
	{
		/// <summary>
		/// Prevents a default instance of the SplintLauncher class from being created.
		/// </summary>
		private SplintLauncher()
		{
		}

		/// <summary>
		/// Launches Splint with the appropriate arguments.
		/// </summary>
		/// <param name="project">The project we are analysing.</param>
		/// <param name="workingDirectoryPath">The working directory of the project.</param>
		/// <param name="splintBinary">Path and name to the Splint binary.</param>
		public static void Launch(WorkSet project, string workingDirectoryPath, string splintBinary)
		{
			ProcessStartInfo startInfo = new ProcessStartInfo();
			string includeFiles = "-I" + string.Join(" -I", project.GetIncludeDirectories());
			string sourceFiles = string.Join(" ", project.SourceFiles.ToArray());
			string definitions = "-D" + string.Join(" -D", project.GetDefinitions());

			startInfo.FileName = splintBinary;
			startInfo.WorkingDirectory = project.RootDir;
			startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
			startInfo.UseShellExecute = false;
			startInfo.CreateNoWindow = false;
			startInfo.Arguments =
				"+f " +
				workingDirectoryPath +
				"\\splint.settings " +
				definitions +
				" " +
				includeFiles +
				" " +
				sourceFiles;

			try
			{
				Process listFiles = Process.Start(startInfo);

				while (!listFiles.HasExited)
				{
				}
			}
			catch
			{
				throw;
			}
		}
	}
}
